package com.bw.es.sync;

import com.alibaba.fastjson.JSON;
import com.bw.common.domain.Shop;
import com.bw.common.result.Result;
import com.bw.es.feign.RemoteShopService;
import com.bw.es.service.EsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Project boot-month
 * @Author 郝昀德
 * @Date 2023/9/6 10:55
 * @Description
 */
@Component
@Log4j2
public class SyncBlog2Es {

    @Autowired
    private RemoteShopService remoteShopService;
    @Autowired
    private EsService esService;

    @Scheduled(cron = "0/5 * * * * ?")
    public void SyncBlog2EsPerMinute(){
        log.info("定时器---->博客从mysql全量同步到es的定时器正在执行,开始查询数据库...");
        Result<List<Shop>> result = remoteShopService.findList();
        List<Shop> list = result.getData();
        log.info("定时器---->博客从mysql全量同步到es的定时器正在执行,mysql查询到的博客数据为:{},开始同步...",
                JSON.toJSONString(list));
        try {
            esService.addEs(list);
            log.info("定时器---->博客从mysql全量同步到es的定时器正在执行,同步成功...");
        } catch (Exception exception) {
            exception.printStackTrace();
            log.error("定时器---->博客从mysql全量同步到es的定时器正在执行,同步失败!"+exception.getMessage());
        }
    }
}
